import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutterproject/controller/sms_code_controller.dart';
import 'package:flutterproject/mvvm/base/base_controller.dart';
import 'package:flutterproject/page/login_all/forget_pwd/children/forget_pwd_set/forget_pwd_set_page.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class ForgetPwdController extends BaseController {
  bool isPhoneCorrect = false;
  bool isSendCode = false;
  int countDown = 60;

  TextEditingController textEditingController = TextEditingController();
  TextEditingController codeEditingController = TextEditingController();

  void onChange() {
    isPhoneCorrect = textEditingController.text.length == 11;
    update(['next']);
  }

  Timer? _timer;

  Future<void> sendCode() async {
    if (isSendCode) return;
    final result = await Get.find<SmsCodeController>()
        .sendCode(mobile: textEditingController.text, type: 2);
    if (!result) return;
    isSendCode = true;
    resetTimer();
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (countDown == 0) {
        isSendCode = false;
        countDown = 60;
        _timer?.cancel();
        _timer = null;
      }
      countDown--;
      state1(() {});
    });
  }

  void resetTimer() {
    countDown = 60;
    _timer?.cancel();
    _timer = null;
  }

  late StateSetter state1;

  void showConfirmDialog() {
    if (textEditingController.text.length != 11) {
      ToastUtil.showTips("请输入11位手机号");
      return;
    }
    DialogUtil.showCustomDialog(
        Builder(builder: (context) {
          return AnimatedPadding(
            padding: MediaQuery.of(context).viewInsets,
            duration: const Duration(milliseconds: 100),
            child: StatefulBuilder(builder: (context, state) {
              state1 = state;
              return Container(
                width: Get.width,
                padding: MarginPaddingUtil.only(top: 41, bottom: 36),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(12.sp),
                    topRight: Radius.circular(12.sp),
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      children: [
                        SizedBox.shrink(),
                        Spacer(),
                        TextCommon(
                          "安全验证",
                          size: 36.sp,
                          color: Color(0xFF242429),
                        ),
                        Spacer(),
                        TextCommon(
                          "取消",
                          size: 24.sp,
                          color: Color(0xFF707070),
                        ).addClickEvent(() {
                          DialogUtil.dismiss();
                        }),
                        48.widthBox,
                      ],
                    ),
                    21.heightBox,
                    Divider(
                      color: Color(0xFFE4E9EB),
                    ),
                    28.heightBox,
                    Padding(
                      padding: MarginPaddingUtil.only(left: 61),
                      child: TextCommon(
                        "${textEditingController.text.substring(0, 3)}****${textEditingController.text.substring(7)}",
                        size: 28.sp,
                        color: Color(0xFF242429),
                      ),
                    ),
                    40.heightBox,
                    Padding(
                      padding: MarginPaddingUtil.only(left: 61),
                      child: Row(
                        children: [
                          Expanded(
                            child: TextField(
                              controller: codeEditingController,
                              decoration: InputDecoration(
                                hintText: "请输入验证码",
                                hintStyle: TextStyle(
                                    fontSize: 28.sp, color: Color(0xFF6C727D)),
                                border: InputBorder.none,
                                counterText: "",
                              ),
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 28.sp,
                                  backgroundColor: Colors.transparent),
                              onChanged: (value) {},
                            ),
                          ),
                          TextCommon(
                            isSendCode ? "$countDown(S)" : "发送验证码",
                            size: 24.sp,
                            color: Color(0xFFEDC88B),
                          ).addClickEvent(() {
                            sendCode();
                          }),
                          40.widthBox,
                        ],
                      ),
                    ),
                    Divider(
                      color: Color(0xFFE4E9EB),
                    ),
                    28.heightBox,
                    ButtonCommon(
                      text: "确认",
                      onTap: () async {
                        if (!isSendCode) {
                          ToastUtil.showTips("请先发送验证码");
                          return;
                        }
                        if (codeEditingController.text.length < 6) {
                          ToastUtil.showTips("请输入验证码");
                          return;
                        }
                        await DialogUtil.dismiss();
                        Get.off(() => ForgetPwdSetPage(), arguments: {
                          "code": codeEditingController.text,
                          "mobile": textEditingController.text
                        });
                      },
                      fontSize: 32.sp,
                      fontColor: Colors.white,
                      color: Color(0xFFEDC88B),
                      borderRadios: 50,
                      padding: MarginPaddingUtil.only(top: 25, bottom: 29),
                      margin: MarginPaddingUtil.symmetric(horizontal: 40),
                    ),
                  ],
                ),
              );
            }),
          );
        }),
        alignment: Alignment.bottomCenter,
        onDismiss: () {
          resetTimer();
        });
  }
}
